package demo1;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ASUS
 * Date: 2024-03-08
 * Time: 10:32
 */
public class MyStack {
    private int[] elem;
    private int usedSize;
    public MyStack() {
        this.elem = new int[5];
    }
    public void push(int val) {
        if (isFull()){
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize] =val;
        usedSize++;
    }

    public boolean isFull() {
        return usedSize == elem.length;
    }

    public int pop() {
        if (empty()){
            throw new StackEmptyException("栈为空");
        }
        return elem[--usedSize];
    }


    public int peek() {
        if (empty()){
            throw new StackEmptyException("栈为空");
        }
        return elem[usedSize];
    }
    public boolean empty(){

            return usedSize == 0;
    }
}
